
import Taro, {Component} from '@tarojs/taro'
import {View} from '@tarojs/components'
import {IStoreProps} from '../../store/Stores'
import {inject, observer} from '@tarojs/mobx'
import {getRuntime} from "../../utils/common";



interface IHomeProps extends IStoreProps {}

@inject("stores")
@observer
class CheckLogin extends Component<IHomeProps> {
  componentWillMount() {
    Taro.setNavigationBarTitle({title: '跳转'})
    this.getUserInfoFromHost()
  }

  // 获取用户信息
  // 1、没关注去关注
  // 2、没token去登录
  // 3、没头像、昵称去登录授权
  getUserInfoFromHost() {
    console.log('mount')
    let { userStore } = this.props.stores
    let cacheUserInfo = getRuntime().getUserInfo() || {}
    let token = cacheUserInfo.cacheUserInfo
    if(!cacheUserInfo || !cacheUserInfo.token) { getRuntime().navigateTo('/pages/other/bindPhone') }
    userStore.getUserInfo({
      callback: res => {
        // getRuntime().setStorage('user', res) // 此处返回的信息未带token
        let userInfo = res || {}
        console.log(userInfo)
        if(Object.keys(userInfo).length<=0) {  getRuntime().navigateTo('/pages/other/bindPhone'); }
        if(userInfo.userState===0){
          getRuntime().showToast('您的登录已过期或是已被禁用，请重新登录~')
          setTimeout(() => {
            getRuntime().navigateTo('/pages/other/bindPhone')
          }, 2000)
        }
        if(!userInfo.subscribe || userInfo.subscribe == '0') {
          // 关注之后怎么告诉服务器已关注?
          Taro.showModal({
            title: '关注我们',
            content: '检测到您还未关注公众号,请前往关注(页面跳转后长按识别二维码关注)',
            showCancel: false,
            confirmText: '立即关注'
          }).then(() => {
            getRuntime().navigateTo('/pages/login/qrcode');
          })
        }

        if(!userInfo.avatar) {
          getRuntime().navigateTo('/pages/other/bindPhone');
        }
        // 缓存用户信息，每次进入首页都更新
        //res.token = getRuntime().getUserInfo().token
        //getRuntime().setUserInfo(res)
      }
    })
  }

  componentDidShow(): void {
    getRuntime().navigateTo('/pages/index/index')
    console.log('show')
  }

  render() {
    return (
      <View className='none'>
        <View className='none-tips'>没有登录或没有授权都会跳到这页</View>
      </View>
    )
  }
}

export default CheckLogin
